package com.example.tesecond.controller;

import com.example.tesecond.entity.User;
import com.example.tesecond.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    UserService userService;
    @RequestMapping("/list")
    public String  list(Model model){
        List <User> users= userService.findAll();
       model.addAttribute("users",users);
        return  "index";
    }

    @RequestMapping("/")
    public  String index(){
        return  "redirect:/list";
    }

    @RequestMapping("/Toadd")
    public String   Toadd(){
        return  "add";
    }
    @RequestMapping("/add")
    public  String   add(String  username,String  content){
        userService.add(username,content);
        return "redirect:/list";
    }

    @RequestMapping("/delete")
    public String  delete(int id){
        userService.delete(id);
        return "redirect:/list";
    }

    @RequestMapping("/toEdit")
    public String  toedit(Model model,String  id){
        int newid=Integer.parseInt(id);
        User user=userService.findById(newid);
        model.addAttribute("user",user);
        return  "update";

    }

    @RequestMapping("/edit")
    public String  edit(String  id,String username,String content){
        int newid=Integer.parseInt(id);
        System.out.println("打印值");
        System.out.println("id"+newid+"username"+username+content);
        User user=new User();
        user.setId(newid);
        user.setUsername(username);
        user.setContent(content);
      userService.update(username,content,newid);
        return "redirect:/list";
    }



}
